<template>
  <div class="header">
    <div class="header_content">
      <div class="search_input_wrapper">
        <el-input
          v-model="searchValue"
          style="height: 100%; width: 100%"
          clearable
          placeholder="请输入内容"
          class="input-with-select"
          @clear="searchClear"
        >
          <template #prepend>
            <el-select
              v-model="selectOptaion"
              :placeholder="getLabelByValue(selectOptaion)"
              style="width: 110px; height: 40px; background: #fff"
            >
              <el-option
                v-for="item in selectOptaionList"
                :key="item.value"
                :value="item.value"
                :label="item.label"
              ></el-option>
            </el-select>
          </template>
          <template #append>
            <el-button
              :icon="Search"
              type="primary"
              size="large"
              color="var(--el-color-primary)"
              @click="debouncedSearch"
            />
          </template>
        </el-input>
      </div>
      <Setting />
    </div>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import Setting from './Setting.vue'
import { debounce } from 'lodash'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
const route = useRoute()
const { keywordType, keyword } = route.query
const selectOptaionList = ref([
  {
    value: 'issn',
    label: 'ISSN',
  },
  {
    value: 'title',
    label: '期刊名称',
  },
  {
    value: 'abbr',
    label: '简称',
  },
  {
    value: 'category',
    label: '学科分类',
  },
])
const props = defineProps({
  keyword: {
    type: String,
    default: '',
  },
  keywordType: {
    type: String,
    default: '',
  },
})
// 获取搜索类型对应的label值
const getLabelByValue = (value) => {
  const item = selectOptaionList.value.find((item) => item.value === value)
  return item ? item.label : ''
}
// 搜索类型列表
const selectOptaion = ref(keywordType || 'title')
const searchValue = ref(keyword)
const emits = defineEmits(['search'])

const debouncedSearch = debounce(() => {
  if (searchValue.value.trim() === '') {
    return ElMessage.warning('请输入搜索内容')
  }
  handleSearch()
}, 300)
const handleSearch = () => {
  emits('search', searchValue.value, selectOptaion.value)
}
</script>
<style scoped lang="scss">
.header {
  -webkit-box-flex: 0;
  -ms-flex: none;
  flex: none;
  width: 100%;
  height: 138px;
  -webkit-box-shadow: 0 5px 7px rgba(0, 0, 0, 0.1);
  box-shadow: 0 5px 7px rgba(0, 0, 0, 0.1);
  background-color: #fff;
}
.header_content {
  box-sizing: border-box;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
  -ms-flex-direction: column;
  flex-direction: column;
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: justify;
  -ms-flex-pack: justify;
  justify-content: space-between;
  width: 1280px;
  height: 100%;
  margin: 0 auto;
  padding: 20px 50px 14px;
  .search_input_wrapper {
    width: 800px;
    height: 48px;
    border: 1px solid var(--el-color-primary);
  }
}
</style>
